﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace SqlConnectionHandler
{
    public static
         class StringExtensions
    {


        public static string Encrypt(this string str, string key = "TCSmartFrameWork.DataAccess!")
        {
            return EncryptSqlConnection(str, key);
        }

        public static string Decrypt(this string str, string key = "TCSmartFrameWork.DataAccess!")
        {
            return DecryptSqlConnection(str, key);
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="text">字符串</param>
        /// <param name="key">key</param>
        /// <returns>加密后字符串</returns>
        internal static string EncryptSqlConnection(string text, string key = "TCSmartFrameWork.DataAccess!")
        {
            string result;
            if (string.IsNullOrEmpty(text))
            {
                result = string.Empty;
            }
            else
            {
                //string s = AG4v5Qk7f4x2nQa9("VABDAFMAbQBhAHIAdABGAHIAYQBtAGUAVwBvAHIAawAuAEQAYQB0AGEAQQBjAGMAZQBzAHMAIQA=");
                MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
                byte[] keyByte = provider.ComputeHash(Encoding.Unicode.GetBytes(key));
                provider.Clear();
                TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider
                {
                    Key = keyByte,
                    Mode = CipherMode.ECB
                };
                byte[] bytes = Encoding.Unicode.GetBytes(text);
                result = Convert.ToBase64String(tripleDESCryptoServiceProvider.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length));
            }
            return result;
        }


        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="text">加密字符串</param>
        /// <param name="key">key</param>
        /// <returns>原始字符串</returns>
        internal static string DecryptSqlConnection(string text, string key = "TCSmartFrameWork.DataAccess!")
        {
            string result;
            if (string.IsNullOrEmpty(text))
            {
                result = string.Empty;
            }
            else
            {
                MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
                byte[] keyByte = provider.ComputeHash(Encoding.Unicode.GetBytes(key));
                provider.Clear();
                TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider
                {
                    Key = keyByte,
                    Mode = CipherMode.ECB
                };
                byte[] inputBuffer = Convert.FromBase64String(text);
                result = Encoding.Unicode.GetString(tripleDESCryptoServiceProvider.CreateDecryptor().TransformFinalBlock(inputBuffer, 0, inputBuffer.Length));
            }
            return result;
        }

    }
}
